package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.enums.CommonStatusEnum;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizGatewayConfigItem;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizGatewayConfigItemMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizGatewayConfigItemParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayConfigItemService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 网关配置项表 服务实现类
 *
 * @author ssy
 * @date 2022/10/31 15:29
 */
@Service
public class BizGatewayConfigItemServiceImpl extends ServiceImpl<BizGatewayConfigItemMapper, BizGatewayConfigItem> implements BizGatewayConfigItemService {

    @Override
    public PageResult<BizGatewayConfigItem> page(BizGatewayConfigItemParam bizGatewayConfigItemParam) {

        // 构造条件
        LambdaQueryWrapper<BizGatewayConfigItem> queryWrapper = new LambdaQueryWrapper<>();
        Page<BizGatewayConfigItem> page = PageFactory.defaultPage();

        if(bizGatewayConfigItemParam!=null && ObjectUtil.isNotEmpty(bizGatewayConfigItemParam.getGatewayConfigCode())){
            queryWrapper.eq(BizGatewayConfigItem::getGatewayConfigCode,bizGatewayConfigItemParam.getGatewayConfigCode());
        }
        if(bizGatewayConfigItemParam!=null && ObjectUtil.isNotEmpty(bizGatewayConfigItemParam.getConfigName())){
            queryWrapper.like(BizGatewayConfigItem::getConfigName,bizGatewayConfigItemParam.getConfigName());
        }
        if(bizGatewayConfigItemParam!=null && ObjectUtil.isNotEmpty(bizGatewayConfigItemParam.getGatewayId())){
            queryWrapper.eq(BizGatewayConfigItem::getGatewayId,bizGatewayConfigItemParam.getGatewayId());
            page = this.page(PageFactory.defaultPage(), queryWrapper);
        }
        // 查询分页结果
        return new PageResult<>(page);
    }

    @Override
    public List<BizGatewayConfigItem> list(BizGatewayConfigItemParam bizGatewayConfigItemParam) {

        // 构造条件
        LambdaQueryWrapper<BizGatewayConfigItem> queryWrapper = new LambdaQueryWrapper<>();
        return this.list(queryWrapper);
    }

    @Override
    public void add(BizGatewayConfigItemParam bizGatewayConfigItemParam) {

        // 校验参数，检查是否存在重复的编码或者名称，不排除当前添加的这条记录
        checkParam(bizGatewayConfigItemParam, false);

        // 将dto转为实体
        BizGatewayConfigItem bizGatewayConfigItem = new BizGatewayConfigItem();
        BeanUtil.copyProperties(bizGatewayConfigItemParam, bizGatewayConfigItem);
        this.save(bizGatewayConfigItem);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizGatewayConfigItemParam bizGatewayConfigItemParam) {
        this.removeById(bizGatewayConfigItemParam.getId());
    }

    @Override
    public void edit(BizGatewayConfigItemParam bizGatewayConfigItemParam) {

        // 根据id查询实体
        BizGatewayConfigItem bizGatewayConfigItem = this.queryBizGatewayConfigItem(bizGatewayConfigItemParam);
        // 校验参数，检查是否存在重复的编码或者名称，排除当前编辑的这条记录
        checkParam(bizGatewayConfigItemParam, true);
        // 请求参数转化为实体
        BeanUtil.copyProperties(bizGatewayConfigItemParam, bizGatewayConfigItem);
        this.updateById(bizGatewayConfigItem);
    }

    @Override
    public BizGatewayConfigItem detail(BizGatewayConfigItemParam bizGatewayConfigItemParam) {
        return this.queryBizGatewayConfigItem(bizGatewayConfigItemParam);
    }

    @Override
    public void changeStatus(BizGatewayConfigItemParam bizGatewayConfigItemParam) {
        Integer id = bizGatewayConfigItemParam.getId();
        Integer status = bizGatewayConfigItemParam.getStatus();

        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<BizGatewayConfigItem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
            .eq(BizGatewayConfigItem::getId, id)
            .set(BizGatewayConfigItem::getStatus, status);
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }

    @Override
    public BizGatewayConfigItem getGatewayConfig(String gatewayCode) {
        return this.baseMapper.getGatewayConfig(gatewayCode);
    }

    /**
     * 校验参数，检查是否存在重复的编码等
     *
     * @author ssy
     * @since 2022/10/31 15:29
     */
    private void checkParam(BizGatewayConfigItemParam bizGatewayConfigItemParam, boolean isExcludeSelf) {
        Integer id = bizGatewayConfigItemParam.getId();
        String code = bizGatewayConfigItemParam.getCode();
        Integer gatewayId = bizGatewayConfigItemParam.getGatewayId();
        // 构建带code的查询条件
        LambdaQueryWrapper<BizGatewayConfigItem> queryWrapperByCode = new LambdaQueryWrapper<>();
        queryWrapperByCode.eq(BizGatewayConfigItem::getCode, code);
        queryWrapperByCode.eq(BizGatewayConfigItem::getGatewayId, gatewayId);

        // 如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            queryWrapperByCode.ne(BizGatewayConfigItem::getId, id);
        }

        // 查询重复记录的数量
        int countByCode = this.count(queryWrapperByCode);

        // 如果存在重复的记录，抛出异常，直接返回前端
        if (countByCode >= 1) {
            throw new ServiceException(500,"该编码已存在");
        }
    }

    /**
     * 获取网关配置项表
     *
     * @author ssy
     * @since 2022/10/31 15:29
     */
    private BizGatewayConfigItem queryBizGatewayConfigItem(BizGatewayConfigItemParam bizGatewayConfigItemParam) {
        BizGatewayConfigItem bizGatewayConfigItem = this.getById(bizGatewayConfigItemParam.getId());
        return bizGatewayConfigItem;
    }
}
